home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / nccomp / ncobject.pas < prev    next >
Pascal/Delphi Source File  |  1996-04-08  |  12KB  |  471 lines

  1. unit Ncobject;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, NonClt, ExtCtrls;
  8.  
  9. {$R NCButs.res}    { Default Blank Bitmap button, Blank }
  10.                    { Bitmaps contain two button 20 x 18 }
  11.                    { next to each other in a 40 x 18 bitmap }
  12.                    { Left is Up, Right is Down }
  13.  
  14. type
  15.   TNCBlank = class(TNCComponent)
  16.   private
  17.     { Private declarations }
  18.   protected
  19.     { Protected declarations }
  20.   public
  21.     { Public declarations }
  22.     constructor Create(AOwner : TComponent); override;
  23.     procedure RePaint; override;
  24.   published
  25.     { Published declarations }
  26.     property Width default 20;
  27.     property DragBy default True;
  28.   end;
  29.  
  30. type
  31.   TNCButton = class(TNCComponent)
  32.   private
  33.     { Private declarations }
  34.     FGlyph : TBitmap;
  35.     FButtonLock : boolean;
  36.     FButtonUp : boolean;
  37.     FOnClick : TNotifyEvent;
  38.     FOnUnClick : TNotifyEvent;
  39.     MouseOnGlyph : boolean;
  40.     ButtonDown : boolean;     {Locked Buttons Only}
  41.     MouseCapture : boolean;   {Normal Buttons Only}
  42.     procedure SetGlyph(Value : TBitmap);
  43.     procedure Paint(Up : boolean);
  44.     procedure ButtonClicked;
  45.     procedure ButtonUnClicked;
  46.   protected
  47.     { Protected declarations }
  48.   public
  49.     { Public declarations }
  50.     constructor Create(AOwner : TComponent); override;
  51.     destructor Destroy;
  52.     procedure RePaint; override;
  53.     procedure MouseMove(MouseX, MouseY : integer); override;
  54.     procedure LButton(ButtonState : TNCClickState); override;
  55.   published
  56.     { Published declarations }
  57.     property Glyph: TBitmap read FGlyph write SetGlyph;
  58.     property ButtonLock: boolean read FButtonLock write FButtonLock
  59.                                             default False;
  60.     property OnClick: TNotifyEvent read FOnClick write FOnClick;
  61.     property OnUnClick: TNotifyEvent read FOnUnClick write FOnUnClick;
  62.     property Width default 20;
  63.   end;
  64.  
  65. type
  66.   TTextAlign = (taLeft,taCenter,taRight);
  67.   TNCLabel = class(TNCBlank)
  68.   private
  69.     { Private declarations }
  70.     FCaption : TCaption;
  71.     FBkColor : TColor;
  72.     FTextColor : TColor;
  73.     FTextAlign : word;
  74.     FFCC : boolean;
  75.     function GetCaption : TCaption;
  76.     procedure SetCaption(Value: TCaption);
  77.     procedure SetFTextColor(Value : TColor);
  78.     procedure SetFBkColor(Value : TColor);
  79.     procedure SetTextAlign(Value : TTextAlign);
  80.     function GetTextAlign : TTextAlign;
  81.     procedure Paint;
  82.   protected
  83.     { Protected declarations }
  84.   public
  85.     { Public declarations }
  86.     constructor Create(AOwner : TComponent); override;
  87.     procedure RePaint; override;
  88.   published
  89.     { Published declarations }
  90.     property Width default 100;
  91.     property Caption : TCaption read GetCaption write SetCaption;
  92.     property TextColor : TColor read FTextColor write SetFTextColor
  93.                                 default clCaptionText;
  94.     property BackgroundColor : TColor read FBkColor write SetFBkColor
  95.                                  default clActiveCaption;
  96.     property TextAlign : TTextAlign read GetTextAlign write SetTextAlign
  97.                                     default taCenter;
  98.     property ForceCaptionColor : boolean read FFCC write FFCC default True;
  99.   end;
  100.  
  101. type
  102.   DisplayType = (dtTime, dtDate);
  103.   DisplaySet = set of DisplayType;
  104.   TNCClock = class(TNCLabel)
  105.   private
  106.     { Private declarations }
  107.     FTimer : TTimer;
  108.     FDisplay : DisplaySet;
  109.     FAlarmSet : boolean;
  110.     FAlarm : TDateTime;
  111.     procedure TimerEventHandler(Sender : TObject);
  112.   protected
  113.     { Protected declarations }
  114.   public
  115.     { Public declarations }
  116.     constructor Create(AOwner : TComponent); override;
  117.     destructor Destroy; override;
  118.     procedure RePaint; override;
  119.     procedure EnableTimer;
  120.     procedure DisableTimer;
  121.   published
  122.     { Published declarations }
  123.     property Width default 60;
  124.     property Display : DisplaySet read FDisplay write FDisplay;
  125.   end;
  126.  
  127.  
  128. procedure Register;
  129.  
  130. implementation
  131.  
  132. procedure Register;
  133. begin
  134.   RegisterComponents('Misc.', [TNCButton, TNCBlank]);
  135.   RegisterComponents('Misc.', [TNCLabel, TNCClock]);
  136. end;
  137.  
  138. { ---=== Methods for TNCBlank ===---}
  139.  
  140. constructor TNCBlank.Create(AOwner : TComponent);
  141. begin
  142.   inherited Create(AOwner);
  143.   Width := 20;
  144.   Position := bpRight;
  145.   DragBy := True;
  146. end;
  147.  
  148. procedure TNCBlank.RePaint;
  149. begin
  150.   GetWindowRect(ParentHandle,ParentRect);
  151.   PosLeft := GetPos;
  152.   PosRight := PosLeft + Width;
  153. end;
  154.  
  155. { ---=== Methods for TNCButton ===---}
  156. { Public }
  157. constructor TNCButton.Create(AOwner : TComponent);
  158. begin
  159.   inherited Create(AOwner);
  160.   FGlyph := TBitmap.Create;
  161.   FGlyph.Handle := LoadBitmap(HInstance,'BLANK');
  162.   MouseCapture := False;
  163.   Position := bpRight;
  164.   FButtonLock := False;
  165.   width := 20;
  166. end;
  167.  
  168. destructor TNCButton.Destroy;
  169. begin
  170.   FGlyph.Free;
  171.   inherited Destroy;
  172. end;
  173.  
  174. procedure TNCButton.LButton(ButtonState : TNCClickState);
  175. begin
  176.   if BorderValid then
  177.   begin
  178.     if FButtonLock then
  179.     { If Button is a has ButtonLock }
  180.     begin
  181.       case ButtonState of
  182.       csDown   : begin
  183.                    if ButtonDown then
  184.                    begin
  185.                      Paint(True);
  186.                      ButtonDown := False;
  187.                      ButtonUnClicked;
  188.                    end
  189.                    else
  190.                    begin
  191.                      Paint(False);
  192.                      ButtonDown := True;
  193.                      ButtonClicked;
  194.                    end;
  195.                  end;
  196.       end;
  197.     end
  198.     else
  199.     { If Button is Normal }
  200.     begin
  201.       Paint(ButtonState = csUp);
  202.       if ButtonState = csUp then
  203.       begin
  204.         MouseCapture := False;
  205.         ReleaseCapture;
  206.         if MouseOnGlyph then ButtonClicked;
  207.       end
  208.       else { down }
  209.       begin
  210.         MouseOnGlyph := True;
  211.         MouseCapture := True;
  212.         SetCapture(ParentHandle);
  213.       end;
  214.     end;
  215.   end;
  216. end;
  217.  
  218. procedure TNCButton.MouseMove(MouseX,MouseY : integer);
  219. begin
  220.   if BorderValid and not(FButtonLock) then
  221.   begin
  222.     if IsCovered(MouseX,MouseY) then
  223.     begin
  224.       if not(MouseOnGlyph) then Paint(False);
  225.       MouseOnGlyph := True;
  226.     end
  227.     else
  228.     begin
  229.       if MouseOnGlyph then Paint(True);
  230.       MouseOnGlyph := False;
  231.     end;
  232.   end;
  233. end;
  234.  
  235. procedure TNCButton.RePaint;
  236. var
  237.   P : TPoint;
  238. begin
  239.   if BorderValid then
  240.   begin
  241.     if FButtonLock and MouseCapture then
  242.       Paint(False)
  243.     else
  244.     begin
  245.       GetCursorPos(P);
  246.       if MouseCapture and IsCovered(P.X-ParentRect.Left,
  247.                             P.Y-ParentRect.Top) then
  248.         Paint(False)
  249.       else
  250.         Paint(True);
  251.     end;
  252.   end;
  253. end;
  254.  
  255. { Private }
  256. procedure TNCButton.Paint(Up : boolean);
  257. var
  258.   WndDC,BmDC : hDC;
  259.   OldBmp : HBitmap;
  260.   Pos : integer;
  261. begin
  262.   GetWindowRect(ParentHandle,ParentRect);
  263.   Pos := GetPos;
  264.   WndDC := GetWindowDC(ParentHandle);
  265.   BmDC := CreateCompatibleDC(WndDC);
  266.   OldBmp := SelectObject(BmDC,FGlyph.Handle);
  267.   if Up then
  268.     BitBlt(WndDC,Pos,VerticalOffSet,Width,CaptionHeight,bmDC,0,0,SRCCOPY)
  269.   else
  270.     BitBlt(WndDC,Pos,VerticalOffSet,Width,CaptionHeight,bmDC,Width,0,SRCCOPY);
  271.   SelectObject(bmDC,OldBmp);
  272.   DeleteDC(bmDC);
  273.   ReleaseDC(ParentHandle,WndDC);
  274.   PosLeft := Pos;
  275.   PosRight := Pos+Width;
  276. end;
  277.  
  278. procedure TNCButton.ButtonClicked;
  279. begin
  280.   if Assigned(FOnClick) then FOnClick(Self);
  281. end;
  282.  
  283. procedure TNCButton.ButtonUnClicked;
  284. begin
  285.   if Assigned(FOnUnClick) then FOnUnClick(Self);
  286. end;
  287.  
  288. procedure TNCButton.SetGlyph(Value : TBitmap);
  289. begin
  290.   if FGlyph <> Value then FGlyph.Assign(Value);
  291. end;
  292.  
  293. { ---=== Methods for TNCLabel ===---}
  294. constructor TNCLabel.Create(AOwner : TComponent);
  295. begin
  296.   inherited Create(AOwner);
  297.   Position := bpRight;
  298.   TextAlign := taCenter;
  299.   TextColor := clCaptionText;
  300.   BackgroundColor := clActiveCaption;
  301.   Width := 100;
  302.   ForceCaptionColor := True;
  303. end;
  304.  
  305. procedure TNCLabel.SetCaption(Value: TCaption);
  306. begin
  307.   if FCaption <> Value then
  308.   begin
  309.     FCaption := Value;
  310.     Repaint;
  311.   end;
  312. end;
  313.  
  314. function TNCLabel.GetCaption : TCaption;
  315. begin
  316.   Result := FCaption;
  317. end;
  318.  
  319. procedure TNCLabel.SetFTextColor(Value : TColor);
  320. begin
  321.   if Value <> FTextColor then
  322.     FTextColor := Value;
  323. end;
  324.  
  325. procedure TNCLabel.SetFBkColor(Value : TColor);
  326. begin
  327.   if Value <> FBkColor then
  328.     FBkColor := Value;
  329. end;
  330.  
  331. procedure TNCLabel.SetTextAlign(Value : TTextAlign);
  332. begin
  333.   case Value of
  334.     taLeft       : FTextAlign := 0;
  335.     taCenter     : FTextAlign := 1;
  336.     taRight      : FTextAlign := 2;
  337.   end;
  338. end;
  339.  
  340. function TNCLabel.GetTextAlign : TTextAlign;
  341. begin
  342.   case FTextAlign of
  343.     0          : Result := taLeft;
  344.     1          : Result := taCenter;
  345.     2          : Result := taRight;
  346.   end;
  347. end;
  348.  
  349. procedure TNCLabel.RePaint;
  350. begin
  351.   if BorderValid then Paint;
  352. end;
  353.  
  354. procedure TNCLabel.Paint;
  355. var
  356.   Pos,Pos1,wth : integer;
  357.   WndDC : hDC;
  358.   PCaption : PChar;
  359.   R : TRect;
  360.   TCol,BCol,OldTextColor,OldBkColor : TColorRef;
  361.   Brush : HBrush;
  362. begin
  363.   GetWindowRect(ParentHandle,ParentRect);
  364.   Pos := GetPos;
  365.   try
  366.     WndDC := GetWindowDC(ParentHandle);
  367.     R.Right := Pos + Width;
  368.     R.Left := Pos;
  369.     R.Top := VerticalOffset;
  370.     R.Bottom := CaptionHeight + 2;   { Ok so I dont know where this 2 comes        }
  371.     try                              { and it probably makes this device dependant }
  372.       GetMem(PCaption,length(Caption)+1);
  373.       StrPCopy(PCaption,Caption);
  374.       if ForceCaptionColor then
  375.       begin
  376.         if ParentActive then
  377.         begin
  378.           BCol := GetSysColor(Color_ActiveCaption);
  379.           TCol := GetSysColor(Color_CaptionText);
  380.         end
  381.         else
  382.         begin
  383.           BCol := GetSysColor(Color_InActiveCaption);
  384.           TCol := GetSysColor(Color_InActiveCaptionText);
  385.         end;
  386.       end
  387.       else
  388.       begin
  389.         TCol := ColorToRGB(TextColor);
  390.         BCol := ColorToRGB(BackgroundColor);
  391.       end;
  392.       OldTextColor := SetTextColor(WndDC,TCol);
  393.       OldBkColor := SetBkColor(WndDC,BCol);
  394.       Brush := CreateSolidBrush(BCol);
  395.       FillRect(WndDC,R,Brush);
  396.       DrawText(WndDC,PCaption,-1,R,DT_SINGLELINE or DT_VCENTER or FTextAlign);
  397.     finally
  398.       FreeMem(PCaption,length(Caption)+1);
  399.       SetTextColor(WndDC,OldTextColor);
  400.       SetBkColor(WndDC,OldBkColor);
  401.       DeleteObject(Brush);
  402.     end;
  403.   finally
  404.     ReleaseDC(ParentHandle,WndDC);
  405.   end;
  406.   PosLeft := R.Left;
  407.   PosRight := R.Right;
  408. end;
  409.  
  410. {---=== Methods for TNCClock ===---}
  411.  
  412. constructor TNCClock.Create(AOwner : TComponent);
  413. begin
  414.   inherited Create(AOwner);
  415.   Width := 60;
  416.   Display := [dtTime,dtDate];
  417.   FTimer := TTimer.Create(Self);
  418.   FTimer.Interval := 1000; {1 second}
  419.   FTimer.OnTimer := TimerEventHandler;
  420.   EnableTimer;
  421. end;
  422.  
  423. destructor TNCClock.Destroy;
  424. begin
  425.   FTimer.Free;
  426.   inherited Destroy;
  427. end;
  428.  
  429. procedure TNCClock.TimerEventHandler(Sender : TObject);
  430. begin
  431.   RePaint;
  432. end;
  433.  
  434. procedure TNCClock.RePaint;
  435. var
  436.   CurTime : TDateTime;
  437.   CurDate : TDateTime;
  438.   DateStr,TimeStr : TCaption;
  439. begin
  440.   if FTimer.Enabled then
  441.   begin
  442.     CurTime := Time;
  443.     CurDate := Date;
  444.     DateStr := '';
  445.     TimeStr := '';
  446.     if dtTime in Display then TimeStr := TimeToStr(Time);
  447.     if dtDate in Display then DateStr := DateToStr(Date);
  448.     Caption := DateStr+' '+TimeStr;
  449.     inherited RePaint;
  450.   end;
  451. end;
  452.  
  453. procedure TNCClock.EnableTimer;
  454. begin
  455.   { do not allow enabling of timer at design Time }
  456.   if not(csDesigning in ComponentState) then
  457.   begin
  458.     FTimer.Enabled := True;
  459.     RePaint;
  460.   end;
  461. end;
  462.  
  463. procedure TNCClock.DisableTimer;
  464. begin
  465.   FTimer.Enabled := False;
  466.   RePaint;
  467. end;
  468.  
  469. end.
  470.  
  471.